var tableSortType = 0;

if( document.getElementById && document.createElement && document.childNodes ) {
	var theTable = document.getElementById('canBeSorted');
	for( var i = 0, x = theTable.getElementsByTagName('tr'); x[i]; i++ ) {
		for( var j = 0, y = x[i].getElementsByTagName('td'); y[j]; j++ ) {
			MWJ_monitorButton( y[j], 'mousedown', startDrag );
			y[j].onselectstart = function () { return false; };
		}
	}
}

function startDrag(e,oCode,oButton,oElement) {
	if( oButton != 'left' ) { return; }
	if( !e ) { e = window.event; }
	if( e.preventDefault ) { e.preventDefault(); }

	window.storeMousePos = MWJ_getMouseCoords(e);
	window.mouseIsDown = tableSortType ? oElement : oElement.parentNode;
	MWJ_monitorMouse(mouseismove);
	MWJ_monitorButton( document, 'mouseup', stopDrag );
}

function mouseismove() {
	if( !window.mouseIsDown ) { return; }
	if( !window.falseElement ) {
		window.falseElement = document.createElement('div');
		falseElement.style.position = 'absolute';
		falseElement.style.width = mouseIsDown.offsetWidth + 'px';
		falseElement.style.height = mouseIsDown.offsetHeight + 'px';
		falseElement.style.borderBottom = '1px solid #8E8E8E';        
		falseElement.onselectstart = function () { return false; };
		document.body.appendChild(falseElement);
	}
	var cellXY = MWJ_getPosition( mouseIsDown );
	falseElement.style.left = tableSortType ? ( ((MWJ_getMouse[0]-storeMousePos[0])+cellXY[0])+'px' ) : ( cellXY[0]+'px' );
	falseElement.style.top = tableSortType ? ( cellXY[1]+'px' ) : ( ((MWJ_getMouse[1]-storeMousePos[1])+cellXY[1])+'px' );
}

function stopDrag() {
	if( window.falseElement ) {
		document.onmousemove = null;
		document.body.removeChild(falseElement);
		window.falseElement = false;
		var currentPosition = tableSortType ? ( (MWJ_getMouse[0]-storeMousePos[0])+MWJ_getPosition( mouseIsDown )[0] ) : ( (MWJ_getMouse[1]-storeMousePos[1])+MWJ_getPosition( mouseIsDown )[1] );
		for( var dragCellPos = 0, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'); x[dragCellPos] != mouseIsDown; dragCellPos++ ) { }
		for( var i = 0, x = mouseIsDown.parentNode.getElementsByTagName(tableSortType?'td':'tr'), beforeThis = x.length; x[i]; i++ ) {
			var cellPosition = MWJ_getPosition( x[i] )[tableSortType?0:1];
			if( x[i] != mouseIsDown && currentPosition < cellPosition && !( beforeThis - x.length ) ) {
				beforeThis = i;
			}
		}
		if( beforeThis != dragCellPos + 1 ) {
			switch( tableSortType ) {
				case 0:
					var trs = mouseIsDown.parentNode.getElementsByTagName('tr');
					if( trs[beforeThis] ) {
						mouseIsDown.parentNode.insertBefore(mouseIsDown,trs[beforeThis]);
					}else{
						mouseIsDown.parentNode.appendChild(mouseIsDown);
					}
					break;
				case 1:
					for( var i = 0, y = mouseIsDown.parentNode.parentNode.getElementsByTagName('tr'), oE; oE = y[i]; i++ ) {
						var tds = oE.getElementsByTagName('td');
						if( tds[beforeThis] ){
							oE.insertBefore(tds[dragCellPos],tds[beforeThis]);
						} else {
							oE.appendChild(tds[dragCellPos]);
						}
					}
					break;
				default:
					var tds = mouseIsDown.parentNode.getElementsByTagName('td');
					if( tds[beforeThis] ) {
						mouseIsDown.parentNode.insertBefore(mouseIsDown,tds[beforeThis]);
					} else {
						mouseIsDown.parentNode.appendChild(mouseIsDown);
					}
			}

		}
		var getstr = '';
		var x = new Array();
		for( var i = 0, x = theTable.getElementsByTagName('tr'); x[i]; i++ ) {
			getstr += x[i].id + ',';
		}
		sort(getstr);
	}
	window.mouseIsDown = false;
}